﻿#include "setup3wind.h"
#include "ui_setup3wind.h"
#include <mainwindow.h>
extern intListener doorStatus;
setup3wind::setup3wind(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::setup3wind)
{
    ui->setupUi(this);
    connect(&doorStatus, &intListener::valueChanged,this,&setup3wind::doorStatusChanged,Qt::QueuedConnection);
#ifdef Q_OS_ANDROID
    int fontId = QFontDatabase::addApplicationFont(":/FontStyle/AlibabaPuHuiTi-3-55-Regular.ttf");
    if (fontId != -1) {
        QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
        if (!fontFamilies.isEmpty()) {
            QString familyName = fontFamilies.first();
            QFont font(familyName, 18); // 创建一个QFont对象，设置字体名称和默认大小

            // 使用setFont方法设置字体，传入QFont对象
            ui->pushButton->setFont(font);
            ui->pushButton_2->setFont(font);
            ui->pushButton_3->setFont(font);
            ui->pushButton_4->setFont(font);
            ui->label_9->setFont(font);
            ui->SaveBtn->setFont(font);


            // 如果需要为其他按钮设置不同的大小，可以创建新的QFont对象
            QFont smallerFont(familyName, 16);
            ui->returnBtn->setFont(familyName);
            ui->pushButton_9->setFont(familyName);
        }
    }
#endif
}
void setup3wind::doorStatusChanged(){
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setText("        打开仓门");
    }
    else
    {
        ui->pushButton_9->setText("        关闭仓门");
    }
}

setup3wind::~setup3wind()
{
    delete ui;
}

void setup3wind::showEvent(QShowEvent *event){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select * From StaticNumInfo";
    query.exec(sql);
    while (query.next()) {
        RunningTime=QString::number(query.value("RunningTime").toDouble(),'f',1);
        TestTime=QString::number(query.value("TestTime").toDouble(),'f',1);
        ExpNum=query.value("ExpNum").toString();
        TestSampleNum=query.value("TestSampleNum").toString();
    }

    eqptinfo eqInfo;
    ui->pushButton->setText(QString("设备运行时长 h\n %1").arg(RunningTime));
    ui->pushButton_2->setText(QString("检测时长\n %1").arg(TestTime));
    ui->pushButton_3->setText(QString("实验次数\n %1").arg(ExpNum));
    ui->pushButton_4->setText(QString("总检测样本数\n %1").arg(TestSampleNum));

    tcpClient = TcpClient::getInstance();
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setText("        打开仓门");
    }
    else
    {
        ui->pushButton_9->setText("        关闭仓门");
    }
    ThresholdDetection();
    event->accept();
}

void setup3wind::on_returnBtn_clicked()
{
    this->close();
    emit changeTotalWindow();
}

void setup3wind::on_SaveBtn_clicked()
{
    this->close();
    emit changeTotalWindow();
}

void setup3wind::on_pushButton_9_clicked()
{
    if(doorStatus.get()==0)
    {
        doorStatus.set(1);
        ui->pushButton_9->setText("        关闭仓门");
    }
    else
    {
        doorStatus.set(0);
        ui->pushButton_9->setText("        打开仓门");
    }
}

void setup3wind::ThresholdDetection(){
    eqptinfo eqInfo;
    int orgOper= RunningTime.toDouble();
    int threOper = eqInfo.operThreRead().toInt();
    if(orgOper>threOper){
        ui->pushButton->setStyleSheet("background: #FFF6F0;border-image: url(:/pic/picture/shu5.png);border-radius: 0px 12px 12px 0px;");
        ui->pushButton->setEnabled(true);
    }else {
        ui->pushButton->setStyleSheet("background: #FFF6F0;border-image: url(:/pic/picture/shu1.png);border-radius: 0px 12px 12px 0px;");
        ui->pushButton->setEnabled(false);
    }


    QTime orgDura= QTime::fromString(eqInfo.durahread(), "hh:mm:ss");
    QTime threDura=QTime::fromString(eqInfo.duraThreRead(), "hh:mm:ss");
    if(orgDura>threDura)
    {
        ui->pushButton_2->setStyleSheet("background: #FFF6F0;border-image: url(:/pic/picture/shu5.png);border-radius: 0px 12px 12px 0px;");
        ui->pushButton_2->setEnabled(true);
    }
    else
    {
        ui->pushButton_2->setStyleSheet("background: #FFF6F0;border-image: url(:/pic/picture/shu2.png);border-radius: 0px 12px 12px 0px;");
        ui->pushButton_2->setEnabled(false);
    }

    int orgTimes=ExpNum.toInt();
    int threTimes=eqInfo.timesThreRead().toInt();
    if(orgTimes>threTimes)
    {
        ui->pushButton_3->setStyleSheet("background: #FFF6F0;border-image: url(:/pic/picture/shu5.png);border-radius: 0px 12px 12px 0px;");
        ui->pushButton_3->setEnabled(true);
    }
    else
    {
        ui->pushButton_3->setStyleSheet("background: #FFF6F0;border-image: url(:/pic/picture/shu3.png);border-radius: 0px 12px 12px 0px;");
        ui->pushButton_3->setEnabled(false);
    }

    int orgSampleSum=TestSampleNum.toInt();
    int threSampleSum=eqInfo.sampleSumThreRead().toInt();
    if(orgSampleSum>threSampleSum)
    {
        ui->pushButton_4->setStyleSheet("background: #FFF6F0;border-image: url(:/pic/picture/shu5.png);border-radius: 0px 12px 12px 0px;");
        ui->pushButton_4->setEnabled(true);
    }
    else
    {
        ui->pushButton_4->setStyleSheet("background: #FFF6F0;border-image: url(:/pic/picture/shu4.png);border-radius: 0px 12px 12px 0px;");
        ui->pushButton_4->setEnabled(false);
    }
}

void setup3wind::on_pushButton_clicked()
{
    QMessageBox *box = new QMessageBox(QMessageBox::Question, "提示", "设备运行时长超时,\n请及时检修!\n如有问题,请联系技术支持", QMessageBox::Yes, this);
    box->button(QMessageBox::Yes)->setText("确认");// 将原本显示“Yes”的按钮改为显示“确认”
    box->exec();// 启动对话框，用res变量记录用户最终点选的按钮
}

void setup3wind::on_pushButton_2_clicked()
{
    QMessageBox *box = new QMessageBox(QMessageBox::Question, "提示", "检测时长超时,\n请及时检修!\n如有问题,请联系技术支持", QMessageBox::Yes, this);
    box->button(QMessageBox::Yes)->setText("确认");// 将原本显示“Yes”的按钮改为显示“确认”
    box->exec();// 启动对话框，用res变量记录用户最终点选的按钮
}

void setup3wind::on_pushButton_3_clicked()
{
    QMessageBox *box = new QMessageBox(QMessageBox::Question, "提示", "试验次数超出限定值,\n请及时检修!\n如有问题,请联系技术支持", QMessageBox::Yes, this);
    box->button(QMessageBox::Yes)->setText("确认");// 将原本显示“Yes”的按钮改为显示“确认”
    box->exec();// 启动对话框，用res变量记录用户最终点选的按钮
}

void setup3wind::on_pushButton_4_clicked()
{
    QMessageBox *box = new QMessageBox(QMessageBox::Question, "提示", "总检测样本数超出限定值,\n请及时检修!\n如有问题,请联系技术支持", QMessageBox::Yes, this);
    box->button(QMessageBox::Yes)->setText("确认");// 将原本显示“Yes”的按钮改为显示“确认”
    box->exec();// 启动对话框，用res变量记录用户最终点选的按钮
}
